#!/usr/bin/python
#-*- coding: utf-8 -*-

#!/usr/bin/python
#-*- coding: utf-8 -*-

import sys
reload(sys)
import os
import simplejson
import pexpect
from pyipmi import tools as ipmitool

class Ipmi(object):

    def get_fan_mod_1A_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 1A RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_mod_2A_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 2A RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_mod_3A_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 3A RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_mod_4A_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 4A RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_mod_5A_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 5A RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_mod_6A_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 6A RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_mod_1B_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 1B RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_mod_2B_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 2B RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_mod_3B_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 3B RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_mod_4B_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 4B RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_mod_5B_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 5B RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_mod_6B_rpm(self):
        command = 'ipmitool -I lanplus -H 192.168.0.251 -U root -P p@ssw0rd sensor get "FAN MOD 6B RPM"'
        content, code = pexpect.run(command, withexitstatus=True)
        result = os.popen(command).read()
        str_info = result.split("\n")
        json_dict = simplejson.dumps(str_info)
        dict_info = simplejson.loads(json_dict)
        dict = {}
        for x in dict_info[1:]:
            ret = x.split(':')
            key = ret[0].strip()
            value = ret[0].strip()
            dict[key] = value
            print x

        if code != 0:
            result = {
                'code': code,
                'message': content
            }
        else:
            result = {
                'code': 0,
                'message': 'success',
                'data': {
                    'content': content.strip()
                }
            }
        return result

    def get_fan_all(self):
        dict = {}
        fan1A = self.get_fan_mod_1A_rpm()
        fan2A = self.get_fan_mod_2A_rpm()
        fan3A = self.get_fan_mod_3A_rpm()
        fan4A = self.get_fan_mod_4A_rpm()
        fan5A = self.get_fan_mod_5A_rpm()
        fan6A = self.get_fan_mod_6A_rpm()

        fan1B = self.get_fan_mod_1B_rpm()
        fan2B = self.get_fan_mod_2B_rpm()
        fan3B = self.get_fan_mod_3B_rpm()
        fan4B = self.get_fan_mod_4B_rpm()
        fan5B = self.get_fan_mod_5B_rpm()
        fan6B = self.get_fan_mod_6B_rpm()

        dict['fan1A'] = fan1A
        dict['fan2A'] = fan2A
        dict['fan3A'] = fan3A
        dict['fan4A'] = fan4A
        dict['fan5A'] = fan5A
        dict['fan6A'] = fan6A

        dict['fan1B'] = fan1B
        dict['fan2B'] = fan2B
        dict['fan3B'] = fan3B
        dict['fan4B'] = fan4B
        dict['fan5B'] = fan5B
        dict['fan6B'] = fan6B

        return dict





if __name__ == '__main__':
    a = Ipmi()
    a.get_fan_all()